package com.example.blog.event.describe;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.example.blog.controller.BaseController;
import com.example.blog.domain.ChangelogServer;
import com.example.blog.domain.OperationalLogs;
import com.example.blog.domain.WebsiteAdministration;
import com.example.blog.event.publisher.LoggingApplicationEvent;
import com.example.blog.mapper.PermissionMapper;
import com.example.blog.service.ChangelogServerService;
import com.example.blog.service.WebsiteAdministrationService;
import com.example.blog.service.impl.OperationalLogsServiceImpl;
import com.example.blog.dto.operationalLogs.OperationLogsDto;
import com.example.blog.utils.IpUtil;
import net.dreamlu.mica.ip2region.core.Ip2regionSearcher;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Objects;
import java.util.Optional;

//自定义事件监听实现
@Service
public class LoggingListen extends BaseController{
    OperationalLogsServiceImpl logsService;
    Ip2regionSearcher ip2regionSearcher;
    PermissionMapper permissionMapper;
    ChangelogServerService changelogServerService;
    WebsiteAdministrationService websiteAdministrationService;

    @Autowired
    public LoggingListen(OperationalLogsServiceImpl logsService,
                         Ip2regionSearcher ip2regionSearcher,
                         WebsiteAdministrationService websiteAdministrationService,
                         ChangelogServerService changelogServerService,
                         PermissionMapper permissionMapper) {
        this.logsService = logsService;
        this.ip2regionSearcher = ip2regionSearcher;
        this.changelogServerService = changelogServerService;
        this.websiteAdministrationService = websiteAdministrationService;
    }

    @EventListener
    public void onApplicationEvent(LoggingApplicationEvent event) {
        //记录管理员操作日志
        OperationLogsDto source = (OperationLogsDto) event.getSource();
        OperationalLogs logs = new OperationalLogs();
        Integer type = 1;
        logs.setType(type);
        logs.setUser(source.getCustomer());
        logs.setContent(source.getContent());
        logs.setAddress(ip2regionSearcher.getAddress(source.getIp()));
        logs.setAdmin(source.getAdmin()== null ? "空" : source.getAdmin().getUsername());
        logs.setIp(source.getIp());
        logs.setOutcome(source.getOutcome());
        logsService.save(logs);


//记录访问ip,归属地
        LambdaQueryWrapper<ChangelogServer> queryWrapper = new LambdaQueryWrapper<>();
        ChangelogServer changelogServer = new ChangelogServer();
        String ipAddress = IpUtil.getIpAddress(getHttpInfo());
        ChangelogServer log = changelogServerService.getOne(queryWrapper.eq(ChangelogServer::getId, ipAddress));
        Optional.ofNullable(log)
                .ifPresent(s->{
                    changelogServer.setIp(ipAddress);
                    changelogServer.setAddress(ip2regionSearcher.getAddress(ipAddress));
                    UpdateWrapper<WebsiteAdministration> updateWrapper = new UpdateWrapper<>();
                    updateWrapper.eq("options", "total_number_visits").setSql("content = content + 1");
                    websiteAdministrationService.update(null, updateWrapper);
                });
    }


}
